package com.shujia.dao;

import com.shujia.bean.Score;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


@Component
public class ScoreDaoImpl implements ScoreDao {


    static final String DRIVER = "com.mysql.jdbc.Driver";
    static final String URL = "jdbc:mysql://node1:3306/student";
    static final String USER = "root";
    static final String PASSWORD = "123456";

    static Connection conn;

    static {
        //加载驱动
        try {
            Class.forName(DRIVER);

            //获取连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public List<Score> queryScoreById(String sId) {

        ArrayList<Score> scores = new ArrayList<>();

        String sql = "select * from score where student_id=?";
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString(1, sId);

            ResultSet resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                String student_id = resultSet.getString("student_id");
                String cource_id = resultSet.getString("cource_id");
                Integer sco = resultSet.getInt("score");

                Score score = new Score(student_id, cource_id, sco);

                scores.add(score);
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return scores;
    }
}
